package Table;

import java.awt.Checkbox;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.util.Vector;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Demo {

	private JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Demo window = new Demo();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Demo() {
		initialize();
	}

	private CustJTable getTable() {
		Vector tableContVect1 = new Vector<String>();
		JCheckBox chckbxNewCheckBox1 = new JCheckBox("否1");
		JCheckBox chckbxNewCheckBox2 = new JCheckBox("否2");
		Object l1[] = { "发放单1", "内容1", chckbxNewCheckBox1 };
		Object l2[] = { "发放单2", "内容2", chckbxNewCheckBox2 };
		tableContVect1.add(l1);
		tableContVect1.add(l2);
		Vector colNameVect1 = new Vector<String>();
		colNameVect1.add("a");
		colNameVect1.add("b");
		colNameVect1.add("命名引用导出");

		CustJTable t = new CustJTable(tableContVect1, colNameVect1, 300, 300,
				new int[] { 150, 150, 150 });

		TableColumnModel colmodel = t.getColumnModel();
		// /这个for循环语句设置一个文本框组件，将它添加到表格的单元格，并且设置单元格单击可编辑

		TableColumn tc = colmodel.getColumn(2);
		CheckBoxEditor cellEditor = new CheckBoxEditor(new JCheckBox());
		cellEditor.setClickCountToStart(1);
		tc.setCellEditor(cellEditor);
		
		CheckBoxRenderer render = new CheckBoxRenderer();
		tc.setCellRenderer(render);
		return t;
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {

		final CustJTable t = getTable();
		frame = new JFrame();
		frame.setBounds(100, 100, 591, 445);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);

		JScrollPane scrollPane = new JScrollPane(t);
		scrollPane.setBounds(50, 32, 460, 271);
		frame.getContentPane().add(scrollPane);

		int x = t.getColumnCount();
		int y = t.getRowCount();

		JButton btnNewButton = new JButton("确定");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JCheckBox status =(JCheckBox) t.getModel().getValueAt(t.getSelectedRow(), 2);
				System.out.println(status.isSelected());
			}
		});
		btnNewButton.setBounds(363, 351, 93, 23);
		frame.getContentPane().add(btnNewButton);
	}
}
